package action.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.CategoryBiz;
import biz.FoodBiz;
import biz.impl.CategoryBizImpl;
import biz.impl.FoodBizImpl;
import entity.Category;
import entity.Food;

public class ProductListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pageNum=1;
		int pageSize=8;

		String search=request.getParameter("search")==null?"":request.getParameter("search");
		int categoryid=request.getParameter("categoryid")==null?
				-1:Integer.parseInt(request.getParameter("categoryid"));
		
		if(request.getParameter("pageNum")!=null){
			pageNum=Integer.parseInt(request.getParameter("pageNum"));
		}
		FoodBiz fb= new  FoodBizImpl();
		List<Food> foodlist= fb.getFoodsListByCategoryId(pageNum, pageSize, categoryid, search);
		
		int foodrole = fb.getFoodsListByCategoryIdAndSearch(categoryid, search).size();
		int totalpage=foodrole%8==0?foodrole/8:foodrole/8+1;
		
		CategoryBiz cd = new CategoryBizImpl();
		List<Category> categorylist =cd.getCategory();
		
		String categoryname = request.getParameter("categoryname");
		
		
		request.setAttribute("categoryname", categoryname);
		request.setAttribute("search", search);
		request.setAttribute("categorylist", categorylist);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("foodlist", foodlist);
		request.setAttribute("categoryid", categoryid);
		
		request.getRequestDispatcher("productList.jsp").forward(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
